/*******************************************************************************
 * Copyright (C) 2010 Chris Wroe   
 * 
 *  Modifications to the initial code base are copyright of their
 *  respective authors, or their employers as appropriate.
 * 
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2.1 of
 *  the License, or (at your option) any later version.
 *    
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *    
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 ******************************************************************************/

package uk.co.wroedevelopments.semanticworkbench.reasoner;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.neo4j.api.core.Direction;
import org.neo4j.api.core.Node;
import org.neo4j.api.core.Relationship;
import org.neo4j.api.core.Traverser;
import org.neo4j.impl.shell.apps.Trav;

import uk.co.wroedevelopments.semanticworkbench.GeneralKnowledgeBasedTest;
import uk.co.wroedevelopments.semanticworkbench.store.AxiomType;
import uk.co.wroedevelopments.semanticworkbench.store.IKnowledgeBase;
import uk.co.wroedevelopments.semanticworkbench.store.NodeConstants;
import uk.co.wroedevelopments.semanticworkbench.store.NodePropertyType;
import uk.co.wroedevelopments.semanticworkbench.store.SemanticRelationshipType;

/**
 * @author Chris Wroe
 *
 */
public class CompletionRule3To5 extends CompletionRule {
	
	static final Logger logger = Logger.getLogger(CompletionRule3To5.class);

	public CompletionRule3To5(IKnowledgeBase kb) {
		super(kb);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void run() {
		// get all negative existentials
		Traverser traverser = knowledgeBase.getAllAxiomOfType(AxiomType.EXISTENTIAL_NEGATIVE);
		for(Node negExis : traverser) {
			logger.debug(negExis.getProperty(NodePropertyType.NAME.name()));
			// keep value
			Node testRestriction = negExis.getSingleRelationship(SemanticRelationshipType.RESTRICTION, Direction.OUTGOING).getEndNode();
			logger.debug("Test Restriction: "+knowledgeBase.getNodeName(testRestriction));
			Node testValue = testRestriction.getSingleRelationship(SemanticRelationshipType.VALUE, Direction.OUTGOING).getEndNode();
			logger.debug("Test value: "+ knowledgeBase.getNodeName(testValue));
			// get all relevant roles
			
			
			Traverser roleTraverser = knowledgeBase.treeWalk(testRestriction.getSingleRelationship(SemanticRelationshipType.ATTRIBUTE,Direction.OUTGOING).getEndNode());
			
			for(Node roleNode : roleTraverser) {
				// get all restrictions for those roles
				//List<Node> restrictions = new ArrayList<Node>();
				logger.debug("Role: "+ knowledgeBase.getNodeName(roleNode));
				for(Relationship rel : roleNode.getRelationships(SemanticRelationshipType.ATTRIBUTE, Direction.INCOMING)) {
					Node restriction = rel.getStartNode();
					logger.debug("Restriction: "+ knowledgeBase.getNodeName(restriction));
					Node value = restriction.getSingleRelationship(SemanticRelationshipType.VALUE, Direction.OUTGOING).getEndNode();
			
					// is value same or kind of test value?
					logger.debug("Testing:" +knowledgeBase.getNodeName(value) +" against" + knowledgeBase.getNodeName(value));
					if (knowledgeBase.subsumedBy(value, testValue)) {
						logger.debug("Found something");
					}
				}
				
			}
		}
		
	}

	
	
}
